﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.MessageServiceRef;

    public class InMessage : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected CheckBox chkSystemCreate;
        protected HtmlForm F1224;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRowID;
        protected HtmlHead Head1;
        protected Label lblCreatedDate;
        protected Label lblFromUserID;
        protected Label lblMessageContent;
        protected Label lblMessageInfo;
        protected Label lblMessageTitle;
        protected Label lblMsg;
        protected Label lblSystemCreate;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCreatedDate;
        protected TextBox txtFromUserID;
        protected TextBox txtMessageContent;
        protected TextBox txtMessageTitle;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> rowIDs = new List<string> {
                this.hdnRowID.Value
            };
            try
            {
                if (int.Parse(MessageServiceClientProxy.GetInstance().DeleteUserMessageInfo(rowIDs)) > 0)
                {
                    base.Response.Redirect("InMessageList.aspx?from=edit", false);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("InMessageQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("InMessageList.aspx?from=edit", false);
                    break;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.SetRoleInfo2Form();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Buffer = true;
            base.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1.0);
            base.Response.Expires = 0;
            base.Response.CacheControl = "no-cache";
            base.Response.AddHeader("Pragma", "No-Cache");
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetRoleInfo2Form()
        {
            string roleId = base.Request.QueryString["ID"];
            if (!string.IsNullOrEmpty(roleId))
            {
                UserMessageQueryEntity userMessageQuery = new UserMessageQueryEntity();
                UserMessageInfo userMessageInfo = MessageServiceClientProxy.GetInstance().GetUserMessageInfoByQueryEntity(roleId);
                if (userMessageInfo != null)
                {
                    this.txtMessageTitle.Text = userMessageInfo.MessageTitle;
                    this.txtMessageContent.Text = userMessageInfo.MessageContent;
                    this.chkSystemCreate.Checked = userMessageInfo.SystemCreated == "Y";
                    this.txtCreatedDate.Text = userMessageInfo.CreatedDate.ToString("yyyy-MM-dd");
                    this.txtFromUserID.Text = userMessageInfo.FromUserID;
                    this.hdnRowID.Value = roleId;
                    if (userMessageInfo.IsRead == "N")
                    {
                        userMessageInfo.IsRead = "Y";
                        userMessageInfo.RowID = int.Parse(roleId);
                        LicenseUtils.IsUnreadMessageChange = true;
                        MessageServiceClientProxy.GetInstance().UpdateUserMessageInfo(userMessageInfo);
                    }
                }
            }
        }
    }
}

